<template>
  <div>
    <!-- 总部库存汇总查询 -->
    <TopFilter :list="topFilterList" @resetChange="resetChange" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTables
        ref="table"
        columnsRef="zjAggregateQuery"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="(columns) => (this.columns = columns)"
      >
        <template slot="controls">
          <!-- <el-button size="small" type="text" style="font-size:14px; font-weight:bold;margin-right:20px;">
            <span v-text="`总部库存数合计:${NumberData.totalSumTotal || 0}台,`"></span>
            <span v-text="`订单占用数量合计: ${NumberData.orderSumTotal || 0}台,`"></span>
            <span v-text="`锁定数量合计: ${NumberData.lockSumTotal || 0}台,`"></span>
            <span v-text="`可用数量合计: ${NumberData.availableSumTotal || 0}台`"></span>
          </el-button> -->
          <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTables>
    </Spin>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, vehicleConfig, getFactoryWarehouseList, getWarehouseList, exportExcel, getWarehousing, exporturl } from '@/api/omSaleP/HeadquartersInventory/AggregateQuery';
import { exportExcelFile } from '@/utils/index';
import {
  getCarOEM,
  getBrandListOEM,
  getSeriesListOEM,
  getModelListOEM,
  getPackageListOEM,
  getOptionListDictOEM,
  getWcolorList,
  getIcolorList,
  addCar,
  updateCar,
  getCarExport,
  importCar,
  getExcelList,
  getExcelListV3,
} from '@/api/common/cars/masterDataCars';
export default {
  name: 'AggregateQuery',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {
        xhrAbort: true,
      },
      exportLoading: false,
      NumberData: {
        totalSumTotal: 0,
        orderSumTotal: 0,
        lockSumTotal: 0,
        availableSumTotal: 0,
      },
    };
  },
  components: {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getFactoryList();
    this.getList();
    this.vehicleInfo();
    this.getDictBrandList();
    this.getColors();
    this.getIColors();
    // this.getTotal({});
  },
  methods: {
    resetChange(params) {
      this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'seriesId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'modelId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      const str = this.topFilterList.find((x) => x.fieldName == 'configId');

      this.$refs.TopFilter.RESET_FORM();
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    // 工厂库
    async getFactoryList() {
      const res = await getFactoryWarehouseList();
      if (res.resultCode == 200) {
        res.data.list.map((x) => {
          this.topFilterList.find((item) => item.fieldName === 'warehouseId').itemList.push({ text: x.warehouseName, value: x.warehouseId });
        });
      }
    },

    // //自己加的车型名称
    //  async modelName() {
    //   const res = await getFimodel();
    //   if (res.resultCode === 200) {
    //     res.data.map(x => {
    //       this.topFilterList.find(item => item.fieldName === 'modelName').itemList.push({ text: x.modelName, value: x.modelName });
    //     });
    //   }
    // },

    // 仓库名称
    async getList() {
      const res = await getWarehouseList();
      if (res.resultCode == 200) {
        res.data.list.map((x) => {
          this.topFilterList.find((item) => item.fieldName === 'subWarehouseId').itemList.push({ text: x.warehouseName, value: x.warehouseId });
        });
      }
    },
    async getTotal(data) {
      let res = await getWarehousing(data);
      this.NumberData = {
        totalSumTotal: res.data.totalSumTotal || 0,
        orderSumTotal: res.data.orderSumTotal || 0,
        lockSumTotal: res.data.lockSumTotal || 0,
        availableSumTotal: res.data.availableSumTotal || 0,
      };
    },
    // 车型选择
    async vehicleInfo() {
      let res = await vehicleConfig(0, { isUnlimited: 10041001 });
      this.topFilterList.find((item) => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    // 导出
    async exportHandle() {
      // let json = Object.assign({}, { CODE: 'CODE_0000043' }, this.fetchParams);
      // let res = await exporturl(json);
      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }
      await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true;

          exportExcel(this.fetchParams)
            .then((response) => {
              exportExcelFile(response.data, '总部库存汇总查询');
              this.exportLoading = false;
            })
            .catch((err) => {});
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    changeHandle(params) {
      this.fetchParams.xhrAbort = false;

      this.getTotal(params);
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async getDictBrandList() {
      const res = await getBrandListOEM();
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === 'brandId').itemList = res.data.map((x) => ({ value: x.brandId, text: x.brandName }));
        // this.topFilterList = [...this.topFilterList]
      }
    },
    async changeBrandSelect(brandsid) {
      // if (!brandsid) {
      this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'seriesId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'modelId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      // } else {
      if (brandsid) {
        const res = await getSeriesListOEM(brandsid);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'seriesId').itemList = res.data.map((x) => ({ value: x.seriesId, text: x.seriesName }));
        }
      }
    },
    async changeSeriesSelect(seriessid) {
      const brandsid = this.topFilterList.find((item) => item.fieldName === 'brandId').initialValue;
      this.topFilterList.find((item) => item.fieldName === 'modelId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      if (brandsid && seriessid) {
        const res = await getModelListOEM(brandsid, seriessid);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'modelId').itemList = res.data.map((x) => ({ value: x.modelId, text: x.modelName }));
        }
      }
    },
    async changeModelSelect(modelId) {
      const brandsid = this.topFilterList.find((item) => item.fieldName === 'brandId').initialValue;
      const seriessid = this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue;
      // if (!brandsid || !seriessid || !modelId) {
      this.topFilterList.find((item) => item.fieldName === 'packageId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      // } else {
      if (modelId) {
        const res = await getPackageListOEM(brandsid, seriessid, modelId);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'packageId').itemList = res.data.map((x) => ({ value: x.packageId, text: x.configName }));
        }
      }
      // }
    },
    async changePackageSelect(packageId) {
      const brandsid = this.topFilterList.find((item) => item.fieldName === 'brandId').initialValue;
      const seriessid = this.topFilterList.find((item) => item.fieldName === 'seriesId').initialValue;
      // if (!brandsid || !seriessid || !modelId) {
      this.topFilterList.find((item) => item.fieldName === 'optionId').initialValue = '';
      this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = [];
      // } else {
      if (packageId) {
        const res = await getOptionListDictOEM(packageId);
        if (res.resultCode === 200) {
          this.topFilterList.find((item) => item.fieldName === 'optionId').itemList = res.data.map((x) => ({ value: x.optionId, text: x.optionName }));
        }
      }
      // }
    },
    async getColors() {
      const res = await getWcolorList();
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === 'colorId').itemList = res.data.map((x) => ({ value: x.colorId, text: x.colorName }));
      }
    },
    async getIColors() {
      const res = await getIcolorList();
      if (res.resultCode === 200) {
        this.topFilterList.find((item) => item.fieldName === 'trimColorId').itemList = res.data.map((x) => ({ value: x.colorId, text: x.colorName }));
      }
    },

    createdFrom() {
      return [
        // {
        //   type: 'INPUT_CASCADER',
        //   label: '车系名称',
        //   fieldName: 'vehicleInfo',
        //   placeholder: '请选择',
        //   options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
        //   options: { titles: [ '车系', '车型', '配置', '选装', '内饰','外饰'] },
        //   itemList: []
        // },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brandId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changeBrandSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'seriesId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changeSeriesSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'modelId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changeModelSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '配置',
          fieldName: 'packageId',
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            this.changePackageSelect(val);
          },
        },
        {
          type: 'SELECT',
          label: '选装',
          fieldName: 'optionId',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'SELECT',
          // type: 'INPUT',
          label: '外饰',
          // fieldName: 'colorStr',
          fieldName: 'colorId',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'SELECT',
          // type: 'INPUT',
          label: '内饰',
          fieldName: 'trimColorId',
          // fieldName: 'interiorColor',
          placeholder: '请选择',
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'productCode',
        },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
        },

        // {
        //   type: 'RANGE_DATE',
        //   label: '入库日期',
        //   fieldName: 'beginVehicleCreatedAt|endVehicleCreatedAt',
        //   placeholder: '请选择',
        //   initialValue: [],
        //   dateType: 'exactdaterange'
        // },
        // {
        //   type: 'RANGE_DATE',
        //   label: '生产日期',
        //   fieldName: 'beginProductDate|endProductDate',
        //   placeholder: '请选择',
        //   initialValue: [],
        //   dateType: 'exactdaterange'
        // },
        //    {
        //   type: 'SELECT',
        //   label: '所属公司',
        //   fieldName: 'powerCompany',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('6008')
        // },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '物料编码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          align: 'center',
          width: 200,
          filterType: 'input',
        },
        {
          title: '物料描述',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          align: 'center',
          width: 400,
          filterType: 'input',
        },
        {
          title: '品牌',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },

        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '配置',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
        },
        {
          title: '内饰',
          dataIndex: 'trimCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '外饰',
          dataIndex: 'colorCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },

        // {
        //   title: '产品代码',
        //   dataIndex: 'productCode',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },差一个物料编码
        // {
        //   title: '产品名称',
        //   dataIndex: 'productName',
        //   align: 'center',
        //   width: 400,
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '动力形式',
        //   dataIndex: 'powerType',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('6008'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.powerType, '6008')}</span>;
        //   }
        // },差一个物料编码，物料描述

        {
          title: '总库存数',
          dataIndex: 'totalSum',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '订单占用数量',
          dataIndex: 'orderSum',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        // {
        //   title: '锁定数量',
        //   dataIndex: 'lockSum',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '可用数量',
          dataIndex: 'availableSum',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
      ];
    },
  },
};
</script>
